<?php

namespace app\asking\controller;

use think\Controller;

class Signin extends Controller
{
    public function __construct(){
        parent::__construct();

        $this->SigninModel = model('Signin');
        $this->BusinessModel = model('Business.User');
    }


    // 签到首页数据
    public function index(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');
            $date = $this->request->param('date','','trim');

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            // 获取一个月的第一天和最后一天
            $start = date("Y-m-01",strtotime($date));
            $end = date("Y-m-t",strtotime($date));

            $list = $this->SigninModel
                ->where(['busid' => $busid])
                ->whereTime('createtime','between',[$start,$end])
                ->order('createtime','asc')
                ->select();


            if(!$list){
                $this->error('本月暂无签到记录');
                exit;
            }else{
                $this->success('成功查询签到记录',null,$list);
                exit;
            }
        }
    }


    // 签到
    public function add(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            // 查询今天是否有签到过
            $start = strtotime(date("Y-m-d")."00:00:00");
            $end = strtotime(date("Y-m-d")."23:59:59");

            $check = $this->SigninModel->where(['busid' => $busid])->whereTime('createtime','today')->find();

            if($check){
                // 签过道的提醒不能重复签到
                $this->error('您今天已签到');
                exit;
            }

            // 开启事务
            $this->SigninModel->startTrans();
            $this->BusinessModel->startTrans();

            // 插入签到表
            $SigninData = [
                'busid' => $busid
            ];

            $SigninStatus = $this->SigninModel->save($SigninData);
            if($SigninStatus === FALSE){
                $this->error($this->SigninModel->getError());
                exit;
            }

            // 用户积分
            $point = $business['point'];
            $point = intval($point) >= 0 ? intval($point) : 0;
            $point++;

            $BusinessData = [
                'id' => $busid,
                'point' => $point
            ];

            $BusinessStatus = $this->BusinessModel->isUpdate(true)->save($BusinessData);

            if($BusinessStatus === FALSE){
                $this->SigninModel->rollback();
                $this->error($this->BusinessModel->getError());
                exit;
            }

            if($BusinessStatus === FALSE || $SigninStatus === FALSE){
                $this->BusinessModel->rollback();
                $this->SigninModel->rollback();
                $this->error('签到失败');
                exit;
            }else{
                $this->BusinessModel->commit();
                $this->SigninModel->commit();
                $this->success('签到成功');
                exit;
            }

        }
    }

}
